package com.ihr360.authority.sharing.privilege;

import com.ihr360.authority.sharing.service.AuthUserResourceService;
import com.ihr360.commons.data.util.Pair;
import com.ihr360.validation.ValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GetOnePrivilege<ID> extends AbstractBizClassPrivilege<ID> {
    final Logger logger = LoggerFactory.getLogger(getClass());
    protected ID id;

    public GetOnePrivilege(ID id, AuthUserResourceService authUserResourceService,IAccessControlClass iAccessControlClass) {
        super(iAccessControlClass);
        this.id = id;
        this.method = AccessControlMethod.METHODS.getOne;
        this.authUserResourceService = authUserResourceService;
    }

    public Integer getPrivilege() {
        return privilege;
    }

    public boolean isAccessControl() {
        return accessControl;
    }

    public Integer getPublicity() {
        return publicity;
    }

    public void setPublicity(Integer publicity) {
        this.publicity = publicity;
    }

    public void validate() {
        if (isAccessControl()) {
            ValidationUtils.rejectIfEmpty(privilege, null, "您没有权限获取此数据");
            ValidationUtils.rejectIfZero(Long.valueOf(privilege), null, "您没有权限获取此数据");
        }
    }

    public GetOnePrivilege doValidate() {
        boolean noAccessControl = noAccessControl(this.method);
        privilege = null;
        publicity = null;
        this.accessControl = !noAccessControl;
        if (!noAccessControl) {
            String strId = toStringId(id);
            Pair<Integer, Integer> privilegePublicityPair = authUserResourceService.queryUserResourcePrivilege(iAccessControlClass.getEntityClass(), strId);
            if (privilegePublicityPair != null) {
                privilege = privilegePublicityPair.getFirst();
                publicity = privilegePublicityPair.getSecond();
            }
            validate();
        }
        return this;
    }
}